#include "socket_class.hpp"
#include "connection.hpp"
#include "network.hpp"
#include "tcp_link.hpp"
#include "tlv_type.h"
#include "io.hpp"
#include <iostream>
using std::cout;
using std::endl;



void test()
{
    //客户端初始化
    rocket::connection client(10086,"127.0.0.1");
    //客户端链接服务器
    client.connect_server();
    rocket::tcp_link link(client.get_fd());
    cout<<"server linked"<<endl;
    string user="boom";
    rocket::packet user_packet(rocket::LOGIN_USER,user.size(),user.c_str());
    user_packet.print();
    rocket::tlv user_tlv(user_packet.get_type(),user_packet.get_length(),user_packet.get_data().c_str()); 
    
    //客户端发送用户名给服务端
    int ret1=link.send_tlv(user_tlv);
    cout<<"send_tlv :"<<ret1<<":";
    user_tlv.print();

    rocket::packet salt;
    //客户端接收盐值
    int ret=link.read_packet(salt);

    cout<<"receive :"<<ret<<":"<<endl;
    salt.print();
    //判断type是不是盐值部分
    if(salt.get_type()==rocket::LOGIN_SALT)
    {
        //获取密码
        string password="159174428";
        //结合盐值和密码进行加密，得到密文
        string encrypted=crypt(password.c_str(), salt.get_data().c_str());
        //将密文发送给服务端
        rocket::tlv encrypted_tlv(rocket::LOGIN_CIPHERTEXT,encrypted.size(),encrypted.c_str());
        ret=link.send_tlv(encrypted_tlv);
        cout<<"send_encrypted_tlv :"<<ret<<":";
        encrypted_tlv.print();
    }
    else
    {
        cout<<"type!=LOGIN_SALT"<<endl;
        return ;
    }

    rocket::packet result;
    //客户端接收密文验证结果
    ret=link.read_packet(result);
    cout<<"receive result:"<<ret<<":"<<endl;
    result.print();
    if(result.get_type()==rocket::LOGIN_RESULT)
    {
        cout<<"type=LOGIN_RESULT"<<endl;
    }

    
}



int main()
{
    test();
    return 0;
}

